#
# Created: 2025-07-26
from pycirclize import Circos
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager

#
# 使用系统默认字体（英文推荐）
plt.rcParams["font.family"] = "Times New Roman"  # 通用无衬线字体
plt.rcParams["font.size"] = 12

np.random.seed(0)  # 设置随机种子，确保每次运行结果一致

# 初始化切尔科斯图的扇形区域
sectors = {"A": 10, "B": 15, "C": 12, "D": 20, "E": 15}
circos = Circos(sectors, space=5)  # 创建Circos对象，指定扇形区域和间隔
colors=["#ECA8A9","#74AED4","#D3E2B7","#CFAFD4","#F7C97E"]

i=0
for sector in circos.sectors:  # 循环遍历每个扇形区域
    # 绘制扇形区域的名称（自动继承全局字体设置）
    sector.text(f"Group: {sector.name}", r=105, size=15)
    # 创建棒棒图x坐标位置和随机的y值
    x = np.arange(sector.start, sector.end) + 0.5
    y = np.random.randint(10, 100, len(x))  # 原始值，模拟高度
    y_norm = y / y.max()*0.9  # 归一化到 [0, 1]

    # Track 1: 棒棒糖图层
    track1 = sector.add_track((80, 100), r_pad_ratio=0.1)
    track1.axis()
    track1.xticks([])  # 隐藏x轴刻度
    for xi, yi in zip(x, y_norm):
        print(yi)
        # 火柴棒
        track1.bar([xi],[yi], color="#F7C197",  width=0.1, alpha=0.8,vmax=1)
        # 火柴头
        track1.scatter([xi], [yi], color="#F7C197",
                       s=50, zorder=10,vmax=1)

    # 创建散点图x坐标位置和随机的y值
    x2 = np.arange(sector.start, sector.end) + 0.3
    y2 = np.random.randint(30, 100, len(x2))  # 原始值，模拟高度

    # Track 2: 绘制散点
    track2 = sector.add_track((55, 75), r_pad_ratio=0.1)
    track2.axis()
    # track2.scatter(x2, y2,s=50,color=colors[i])
    track2.line(x2, y2,linewidth=1.5,color=colors[i])

    # 创建散点图x坐标位置和随机的y值
    x3 = np.arange(sector.start, sector.end) + 0.2
    y3 = np.random.randint(20, 90, len(x3))  # 原始值，模拟高度
    # Track 3: 绘制条形图
    track3 = sector.add_track((30, 50), r_pad_ratio=0.1)
    track3.axis()
    track3.bar(x3, y3,width=0.7,facecolor=colors[i],alpha=0.6,linewidth=0.8,edgecolor=colors[i])
    # 如果需要单独控制每个柱子的透明度（可选）

    i+=1

# 绘制连接线（完全保持原样）
circos.link(("A", 0, 3), ("B", 15, 12),color="#b0b7d9", ec="green",lw=2.0, ls="dashed")
circos.link(("B", 0, 3), ("C", 7, 11), color="#80dcfb")
circos.link(("C", 2, 5), ("E", 15, 12), color="#f8c698", direction=1,hatch="//")
circos.link(("D", 3, 5), ("D", 18, 15), color="purple",
            ec="black", lw=0.5, direction=2)
circos.link(("D", 8, 10), ("E", 2, 8), color="violet")

# 输出图像
circos.savefig("circos_fixed.png")
fig = circos.plotfig()
fig.show()

